package model.user;

import model.Database;
import model.UID_Generator;
import net.passClass.PassData;
import net.passClass.PassStaff;
import util.MD5;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Objects;

public class StaffDB implements Database<Staff> {
    private ArrayList<Staff> staffs = new ArrayList<>();
    public ArrayList<Staff> getStaffs() {
        return staffs;
    }
    private static StaffDB instance = null;
    private StaffDB() {}
    private StaffDB(ArrayList<Staff> staffs) {
        if(staffs!=null){
            this.staffs = staffs;
        } else {
            this.staffs = new ArrayList<>();
        }
    }
    public static StaffDB getInstance() {
        if (instance == null){
            instance = new StaffDB();
        }
        return instance;
    }
    public static void initialize(ArrayList<Staff> staffs) {
        if (instance == null) {
            instance = new StaffDB(staffs);
        }
    }

    @Override
    public PassData add(Staff staff){
        for (Staff aStaff : staffs){
            if(!aStaff.isDeleted() &&
                    aStaff.getUserName().equals(staff.getUserName())){
                return new PassData(false, null,"职员重复，添加失败");
            }
        }
        staff.setUid(UID_Generator.getUid("S"));
        staffs.add(staff);
        return new PassData(true, null,"职员添加成功");
    }

    @Override
    public PassData update(Staff staff){
        if(!staffs.contains(staff)){
            return new PassData(false, null,"职员不存在，修改失败");
        }
        Staff theStaff = staffs.get(staffs.indexOf(staff));
        theStaff.setDeleted(true);
        staffs.add(staff);

        return new PassData(true, null,"职员修改成功");
    }

    @Override
    public PassData delete(Staff staff){
        if(!staffs.contains(staff)){
            return new PassData(false, null,"职员不存在，删除失败");
        }
        Staff theStaff = staffs.get(staffs.indexOf(staff));
        theStaff.setDeleted(true);
        return new PassData(true, null,"职员删除成功");
    }

    @Override
    public PassStaff get(Staff staff){
        LinkedList<Staff> tmpAns = new LinkedList<>(staffs);    //保证原数据集合不被删除,而且便于快速删除元素索引
        tmpAns.removeIf(Staff::isDeleted);                      //直接将方法传入

        if(staff.getUid()!=null && !staff.getUid().equals("")){
            if(tmpAns.contains(staff)){
                ArrayList<Staff> ans = new ArrayList<>();
                ans.add(tmpAns.get(tmpAns.indexOf(staff)));
                return new PassStaff(true,ans,"职员查找成功");
            } else{
                return new PassStaff(true,null,"不存在查找结果");
            }
        }

        if(staff.getUserName()!=null){
            tmpAns.removeIf(aStaff -> aStaff.getUserName() == null ||           //lambda表达式
                    !aStaff.getUserName().contains(staff.getUserName()));       //包含字段即可
        }
        if(staff.getName()!=null){
            tmpAns.removeIf(aStaff -> aStaff.getName() == null ||
                    !aStaff.getName().contains(staff.getName()));
        }
        if(staff.getTitle()!=-1){
            tmpAns.removeIf(aStaff -> aStaff.getTitle()==-1 ||
                    aStaff.getTitle() != staff.getTitle());
        }
        if(staff.getBirthday()!=null){
            tmpAns.removeIf(aStaff -> aStaff.getBirthday() == null ||
                    !aStaff.getBirthday().equals(staff.getBirthday()));         //只能对比全部日期
        }
        if(staff.getSpecialty()!=null){
            tmpAns.removeIf(aStaff -> aStaff.getSpecialty() == null ||
                    !aStaff.getSpecialty().contains(staff.getSpecialty()));
        }
        if(staff.getTel()!=null){
            tmpAns.removeIf(aStaff -> aStaff.getTel() == null ||
                    !aStaff.getTel().contains(staff.getTel()));
        }
        if(staff.getIdNumber()!=null){
            tmpAns.removeIf(aStaff -> aStaff.getIdNumber() == null ||
                    !aStaff.getIdNumber().contains(staff.getIdNumber()));
        }
        ArrayList<Staff> ans = new ArrayList<>();
        for(Staff aStaff:tmpAns){
            try {
                ans.add((Staff) aStaff.clone());
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        }

        for (Staff aStaff:ans) {
            aStaff.setPassword(null);       //清除传送的密码信息
        }
        return new PassStaff(true,ans,"职员查找成功");
    }

    public PassData login(User user){
        for (Staff aStaff:staffs){
            if(!aStaff.isDeleted() && aStaff.getUserName().equals(user.getUserName())){
                if(Objects.equals(MD5.md5Encryption(aStaff.getPassword()),
                        user.getPassword())){
                    return new PassData(true,null,"职员登陆成功");
                }
            }
        }
        return new PassData(false,null,"职员登陆失败");
    }

}
